home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6984 / 6984.xpi / chrome / lazarus.jar / content / debug.js < prev    next >
Text File  |  2009-11-24  |  6KB  |  218 lines

  1.  
  2.  
  3.  
  4. function debug(){
  5.     var msg = "";
  6.     for (var i=0; i<arguments.length; i++){
  7.         msg += debug.debugString(arguments[i]) +"\n";
  8.     }
  9.     try {
  10.         var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
  11.         consoleService.logStringMessage(msg);
  12.     }
  13.     catch(e){
  14.         alert(msg)
  15.     }
  16. }
  17.  
  18. debug.MAX_DEPTH = 3;
  19.  
  20. debug.debugString = function(arg, depth){
  21.     
  22.     var undefined;
  23.     
  24.     switch (arg){
  25.     case null:  return "[null]";
  26.     case "":    return "[empty string]";
  27.     case true:  return "[true]";
  28.     case false: return "[false]";
  29.     case undefined: return "[undefined]";
  30.     default:
  31.         var msg = "";
  32.         var type = typeof(arg);
  33.         
  34.         if (type == "function"){
  35.             //just grab the first line of the function
  36.             msg += arg.toString().split(/\n/)[0].replace(/\{/, '');
  37.             break;
  38.         }
  39.         else if (type == "string"){
  40.             msg += '[string:'+ arg.length +'] '+ arg;
  41.         }
  42.         else if (type == "object" && typeof arg.length === 'number' && arg.propertyIsEnumerable && !arg.propertyIsEnumerable('length')){
  43.             msg += "[array:"+ arg.length +"]\n"; 
  44.             depth = depth || 0;
  45.             if (depth < debug.MAX_DEPTH){
  46.                 var tabs ="\t";
  47.                 for (var i=0; i<depth; i++){
  48.                     tabs +="\t";
  49.                 }
  50.                 
  51.                 for (var i=0; i<arg.length; i++){
  52.                     msg += tabs +"["+ i +"]="+ debug.debugString(arg[i], depth+1) +"\n";
  53.                 }
  54.             }
  55.         }
  56.         else if (arg.nodeType && arg.nodeType == 9){
  57.             msg += "[DOMDocument] "+ arg.URL;
  58.         }
  59.         else if (type == "object"){
  60.             switch(arg.nodeType){
  61.                 case 9:
  62.                     return "[HTMLDocument] "+ arg.URL;
  63.                     
  64.                 case 1:
  65.                     msg = "[HTMLElement] "+ arg.nodeName;
  66.                     msg += arg.id ? (" #"+ arg.id) : '';
  67.                     msg += arg.className ? (" ."+ arg.className) : '';
  68.                     msg += arg.href ? (" href="+ arg.href) : '';
  69.                     return msg;
  70.             }
  71.             
  72.             msg += "[object]\n";
  73.             depth = depth || 0;
  74.             if (depth < debug.MAX_DEPTH){
  75.                 var tabs ="\t";
  76.                 for (var i=0; i<depth; i++){
  77.                     tabs +="\t";
  78.                 }
  79.                 
  80.                 for (var prop in arg){
  81.                     try {
  82.                         msg += tabs + prop +"="+ debug.debugString(arg[prop], depth+1) +"\n";
  83.                     }
  84.                     catch(e){
  85.                         msg += tabs + prop +"=?\n"; 
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.         else {
  91.             try {
  92.                 msg += arg +" ["+ type +"]";
  93.             }
  94.             catch(e){
  95.                 msg += "?"+" ["+ type +"]";
  96.             }
  97.         }
  98.     }
  99.     return msg;
  100. }
  101.  
  102. debug.test = function(){
  103.     debug({
  104.         "int": 0,
  105.         "string": "a long string",
  106.         "function": function(){},
  107.         "null": null,
  108.         "true": true,
  109.         "false": false,
  110.         "error": Error("just an error"),
  111.         "array": [1,2,3],
  112.         "object": {
  113.             "x": 123,
  114.             "y": 456
  115.         },
  116.         "more stuff": "cheddar",
  117.         "how deep" : {
  118.             "level 1": {
  119.                 "level 2": {
  120.                     "level 3": {
  121.                     }
  122.                 }
  123.             }
  124.         },
  125.         "empty string": ""
  126.     });
  127. }
  128.  
  129. /**
  130. * open the javascript console
  131. */
  132. debug.openJSConsole = function(){
  133.     window.open("chrome://global/content/console.xul", "_blank", "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar");
  134. }
  135.  
  136. /**
  137. * sends a message to the Javascript Console
  138. */
  139. debug.consoleMsg = function(str){
  140.     Components.utils.reportError(str);
  141. }
  142.  
  143.  
  144. var Profiler = function(title){
  145.     
  146.     //name of this instance
  147.     this.title = title || '';
  148.     
  149.     /**
  150.     * return the current epoc in seconds
  151.     */
  152.     this.timestamp = function(){
  153.         return new Date().getTime() / 1000;
  154.     }
  155.     
  156.     //start time
  157.     this.startTime = this.timestamp();
  158.     
  159.     //array of points marked by code
  160.     this.log = [];
  161.     
  162.     /**
  163.     * mark a point in the code
  164.     */
  165.     this.mark = function(title){
  166.         this.log.push({
  167.             "title": title,
  168.             "time": this.timestamp()
  169.         })    
  170.     }
  171.     
  172.     /**
  173.     * stops the profiler and returns the profiler log as a string
  174.     */
  175.     this.stop = function(title){
  176.         this.mark(title);
  177.         var msg = this.title +"\n";
  178.         var lastTime = this.startTime;
  179.         for (var i=0; i<this.log.length; i++){
  180.             var item = this.log[i];
  181.             var elapsed = item.time - lastTime;
  182.             lastTime = item.time;
  183.             msg += this.fix(elapsed, 3, true) +": "+  item.title +"\n";
  184.             if (i == this.log.length -1){
  185.                 msg += "total time "+ this.fix(item.time - this.startTime, 3, true) +" seconds";
  186.             }
  187.         }
  188.         
  189.         return msg;
  190.     }
  191.     
  192.     /**
  193.     * formats a number to the given decimal places
  194.     */
  195.     this.fix = function(num, dp, returnAsPaddedString){
  196.         dp = dp || 0;
  197.         
  198.         var mult = Math.pow(10, dp);
  199.         num = (Math.round(num * mult) / mult);
  200.         
  201.         if (returnAsPaddedString){
  202.             //make sure num is a string
  203.             var numStr = ""+ num;
  204.             var bits = numStr.split(/\./, 2);
  205.             bits[1] = bits[1] || '';
  206.  
  207.             for (var i=bits[1].length; i<dp; i++){
  208.                 bits[1] += '0';
  209.             }
  210.  
  211.             return bits.join('.');
  212.         }
  213.         else {
  214.             return num;
  215.         }
  216.     }
  217. }
  218.